package com.cos.hospital.ui.usercenter;

import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.code.helper.KTextWatcher;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.user.AssisBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.URegular;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 修改助理
 * @Author: Kosmos
 * @Date: 2018年09月13日 22:52
 * @Email: KosmoSakura@foxmail.com
 */
public class AssistantReviseActivity extends BaseActivity {

    @BindView(R.id.revise_assis_state)
    CheckBox cbState;
    @BindView(R.id.revise_assis_phone)
    EditText edtPhone;
    @BindView(R.id.revise_assis_name)
    TextView tName;
    private String canSave;
    private int assisId;

    @Override
    protected int layout() {
        return R.layout.activity_assistant_revise;
    }

    @Override
    protected void init() {
        setTitleLeftIcon();
        setTitleCenterTxt(getString(R.string.assistant_revise));
        setTitleRightTxt(getString(R.string.save));
        assisId = getIntent().getIntExtra(Code.Actions.AssistantId, -1);
        canSave = checkSavebtn();
    }

    private String checkSavebtn() {
        if (UTxt.isEmpty(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_enter);
        }
        if (!URegular.checkPhoneNum(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_error);
        }
        return "";
    }

    /**
     * 1.姓名栏，不允许修改；
     * 2.手机号码，可以修改，规则参照新增助理手机号码规则；
     * 3.助理状态已开关形式修改；
     * 4.每次修改保存后都将重置密码为默认密码（手机号码后6位）；
     */
    @Override
    protected void logic() {
        edtPhone.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                canSave = checkSavebtn();
            }
        });
        edtPhone.setOnFocusChangeListener((v, hasFocus) -> {
            String phone = edtPhone.getText().toString();
            if (!UTxt.isEmpty(phone) && !phone.contains("*") && !URegular.checkPhoneNum(phone)) {
                showNotice(getString(R.string.phone_number_error));
            }
        });
        getAssisInfo();
    }

    private void getAssisInfo() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", assisId);
        UHttp.start(getServes().assistSearch(jsonObject), "查询助理信息", new HttpMsgListener<ResultBean<AssisBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<AssisBean> dto) {
                AssisBean result = dto.getResult();
                if (result == null) {
                    showNotice("数据异常");
                } else {
                    tName.setText(UTxt.isNull(result.getFldCustomerName()));
                    edtPhone.setText(UTxt.isNull(result.getFldPhone()));
                    cbState.setChecked(result.getFldRelationState() == 1);
                }
            }
        });
    }

    @OnClick({R.id.revise_assis_name, R.id.title_back, R.id.title_next_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.revise_assis_name:
                showNotice("姓名不允许修改");
                break;
            case R.id.title_back:
                finish();
                break;
            case R.id.title_next_txt://新增助理
                if (UTxt.isEmpty(canSave)) {
                    save();
                } else {
                    showNotice(canSave);
                }
                break;
        }
    }

    private void save() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldDoctorId", SPUtil.getUserId());
        jsonObject.addProperty("fldAssistantId", assisId);
        jsonObject.addProperty("fldRelationState", cbState.isChecked() ? 1 : 0);
        jsonObject.addProperty("fldPhone", edtPhone.getText().toString());
        UHttp.start(getServes().assistEdit(jsonObject), "修改助理", new HttpMsgListener<ResultBean>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean dto) {
                if (dto.isSuccess()) {
                    UToast.toast("修改成功");
                    setResult(1000);
                    finish();
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }
}
